/**
 * 数据库管理API服务 - 调用后端真实接口
 */
import { request } from "@/http/axios"

export interface DatabaseConnectionRequest {
  type: "mysql" | "postgresql" | "oracle"
  host: string
  port: number
  database: string
  username: string
  password: string
  charset?: string
  connectTimeout?: number
  useSSL?: boolean
  allowPublicKeyRetrieval?: boolean
}

export interface DatabaseField {
  name: string
  type: string
  length?: number
  precision?: number
  scale?: number
  nullable: boolean
  primaryKey: boolean
  autoIncrement: boolean
  defaultValue?: string
  comment?: string
}

export interface DatabaseTable {
  id?: number
  name: string
  comment?: string
  fields: DatabaseField[]
  createdAt?: string
  updatedAt?: string
}

export interface TableImportRequest {
  connectionConfig: DatabaseConnectionRequest
  tableNames: string[]
  tablePrefix?: string
}

export interface DatabaseConnectionResponse {
  success: boolean
  message?: string
  version?: string
  charset?: string
}

export type DatabaseTablesResponse = ApiResponseData<string[]>
export type DatabaseTableDetailResponse = ApiResponseData<DatabaseTable>
export type DatabaseTableImportResponse = ApiResponseData<{ importedCount: number }>
export type DatabaseConnectionTestResponse = ApiResponseData<DatabaseConnectionResponse>

export class DatabaseApiService {
  /**
   * 测试数据库连接
   */
  static async testConnection(config: DatabaseConnectionRequest): Promise<DatabaseConnectionTestResponse> {
    return request<DatabaseConnectionTestResponse>({
      url: "database/testConnection",
      method: "post",
      data: config
    })
  }

  /**
   * 获取数据库所有表
   */
  static async getTables(config: DatabaseConnectionRequest, tableFilter?: string): Promise<DatabaseTablesResponse> {
    return request<DatabaseTablesResponse>({
      url: "database/tables",
      method: "post",
      data: { ...config, tableFilter }
    })
  }

  /**
   * 获取表详细信息
   */
  static async getTableDetail(config: DatabaseConnectionRequest, tableName: string): Promise<DatabaseTableDetailResponse> {
    return request<DatabaseTableDetailResponse>({
      url: "database/tableDetail",
      method: "post",
      data: { ...config, tableName }
    })
  }

  /**
   * 导入数据表
   */
  static async importTables(importRequest: TableImportRequest): Promise<DatabaseTableImportResponse> {
    return request<DatabaseTableImportResponse>({
      url: "database/importTables",
      method: "post",
      data: importRequest
    })
  }

  /**
   * 获取已导入的表列表
   */
  static async getImportedTables(): Promise<ApiResponseData<DatabaseTable[]>> {
    return request<ApiResponseData<DatabaseTable[]>>({
      url: "database/importedTables",
      method: "get"
    })
  }

  /**
   * 删除已导入的表
   */
  static async deleteImportedTable(tableId: number): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `database/importedTables/${tableId}`,
      method: "delete"
    })
  }

  /**
   * 更新表信息
   */
  static async updateTable(tableId: number, table: Partial<DatabaseTable>): Promise<ApiResponseData<DatabaseTable>> {
    return request<ApiResponseData<DatabaseTable>>({
      url: `database/importedTables/${tableId}`,
      method: "put",
      data: table
    })
  }

  /**
   * 同步表结构
   */
  static async syncTable(tableId: number, config: DatabaseConnectionRequest): Promise<ApiResponseData<DatabaseTable>> {
    return request<ApiResponseData<DatabaseTable>>({
      url: `database/syncTable/${tableId}`,
      method: "post",
      data: config
    })
  }

  /**
   * 获取表关系
   */
  static async getTableRelations(): Promise<ApiResponseData<any[]>> {
    return request<ApiResponseData<any[]>>({
      url: "database/tableRelations",
      method: "get"
    })
  }

  /**
   * 生成DDL语句
   */
  static async generateDDL(tableIds: number[]): Promise<ApiResponseData<{ ddl: string }>> {
    return request<ApiResponseData<{ ddl: string }>>({
      url: "database/generateDDL",
      method: "post",
      data: { tableIds }
    })
  }
}
